package com.scott.crm.resume.controller;


import com.scott.base.BaseBean;
import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.base.utils.AESUtil;
import com.scott.crm.resume.bean.QuestionBean;
import com.scott.crm.resume.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author 黄德标
 * @date 2021/11/29 16:21
 */
@RestController
@RequestMapping("question")
@Slf4j
@CrossOrigin
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    //========================================== 学生增加一个面试问答 ==========================
    /**
     * 方法描述: 【学生】 增加一个面试问题
     * URL: localhost:8080/kaoqin/question/addQuestion
     * {
     *     "question": "测试面试问题",
     *     "studentAnswer":"测试学生回答"
     * }
     * @author: 黄德标
     * @date: 2021/11/23 22:20
     * @param: QuestionBean对象
     */
    @RequestMapping(value = "/addQuestion",method = RequestMethod.POST)
    public DataRes addQuestion(@RequestBody QuestionBean questionBean){

        // 拿到用户令牌
        String token = questionBean.getToken();

        // 解密，拿到userId
        String encrypt = null;
        try {
            encrypt = AESUtil.decrypt(token, AESUtil.KEY);
        } catch (Exception e) {
            return DataUtil.errors();
        }
        int userId = Integer.parseInt(encrypt);

        questionBean.setStudentId(userId);

        // 添加操作
        questionService.addQuestion(questionBean);

        return DataUtil.success("添加成功");
    }
    // ========================================== END =======================================

    //========================================== 老师填写一个面试问答中的老师回答 ==========================
    /**
     * 方法描述: 【老师】 填写 学生的面试问题 的老师答案
     * @author: 黄德标
     * @date: 2021/11/29 21:03
     * @param: QuestionBean对象必须有id、teacherAnswer、token属性
     * @return: com.scott.base.message.DataRes
     */
    @RequestMapping(value = "addQuestionTeacherAnswer",method = RequestMethod.POST)
    public DataRes addQuestionTeacherAnswer(@RequestBody QuestionBean questionBean){
        // 拿到用户令牌
        String token = questionBean.getToken();

        // 解密，拿到userId
        String encrypt = null;
        try {
            encrypt = AESUtil.encrypt(token, AESUtil.KEY);
        } catch (Exception e) {
            return DataUtil.errors();
        }
        int teacherId = Integer.parseInt(encrypt);
        questionBean.setTeacherId(teacherId);

        questionService.addQuestionTeacherAnswer(questionBean);

        return DataUtil.success("填写成功");
    }
    // ========================================== END =======================================

    //========================================== 学生删除一个面试问答记录 ==========================
    /**
     * 方法描述: 【学生】 根据questionBean对象中的 id, 删除自己 在面试中的一个面试问答
     * URL: localhost:8080/kaoqin/question/deleteQuestionById
     * @author: 黄德标
     * @date: 2021/11/29 21:40
     * @param: [questionBean]
     * @return: com.scott.base.message.DataRes
     */
    @RequestMapping(value = "deleteQuestionById",method = RequestMethod.POST)
    public DataRes deleteQuestionById(@RequestBody QuestionBean questionBean){
        Integer rs = questionService.deleteQuestionByQuestionId(questionBean);
        if (rs == -1){
            return DataUtil.errors();
        }else {
            return DataUtil.success("删除成功");
        }
    }


    //========================================== 学生查看自己所有的面试问答 ==========================
    /**
     * 方法描述: 【学生】 查找 自己 所有面试 的所有问题
     * URL: localhost:8080/kaoqin/question/selectQuestionBeanByStudentId
     * @author: 黄德标
     * @date: 2021/11/29 20:16
     * @param: [com.scott.base.BaseBean]
     */
    @RequestMapping(value = "selectQuestionBeanByStudentId",method = RequestMethod.GET)
    public DataRes selectQuestionBeanByStudentId(@RequestBody BaseBean baseBean){

        String stringStudentId = null;

        try {
            stringStudentId = AESUtil.decrypt(baseBean.getToken(),AESUtil.KEY);
        } catch (Exception e) {
            return DataUtil.errors();
        }

        int studentId = Integer.parseInt(stringStudentId);

        ArrayList<QuestionBean> questionBeanArrayList
                = questionService.selectQuestionBeanByStudentId(studentId);

        return DataUtil.success(questionBeanArrayList);
    }
    // ========================================== END =======================================

    //==========================================【学生】、【老师】 查看自己本班的所有的面试问答 ==========================
    /**
     * 方法描述: 查询 用户的班级的 所有面试问题
     * @author: 黄德标
     * @date: 2021/11/29 20:51
     * @param: [com.scott.base.BaseBean]
     * @return: com.scott.base.message.DataRes
     */
    @RequestMapping(value = "selectAllQuestionBean",method = RequestMethod.GET) //     3J/l6WRMJ7EaOIHcN6jFLA==
    public DataRes selectAllQuestionBean(BaseBean baseBean){
        String stringUserId = null;

        try {
            stringUserId = AESUtil.decrypt(baseBean.getToken(),AESUtil.KEY);
        } catch (Exception e) {
            return DataUtil.errors();
        }
        int userId = Integer.parseInt(stringUserId);

        ArrayList<QuestionBean> questionBeanArrayList
                = questionService.selectQuestionBeanByUserId(userId);

        return DataUtil.success(questionBeanArrayList);
    }
    // ========================================== END =======================================

    //==========================================【管理员、校长】 查看所有的面试问答 ==========================
    @RequestMapping(value = "selectAll",method = RequestMethod.POST)
    public DataRes selectAll(@RequestBody BaseBean baseBean){
        String stringUserId = null;
        try {
            stringUserId = AESUtil.decrypt(baseBean.getToken(),AESUtil.KEY);
        } catch (Exception e) {
            return DataUtil.errors();
        }
        int userId = Integer.parseInt(stringUserId);
        // 判断userId 是否为校级管理员

        ArrayList<QuestionBean> questionBeanArrayList
                = questionService.selectAll();

        return DataUtil.success(questionBeanArrayList);
    }


}
